﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Sisocana.PhoneApp.MobileServices;

namespace Sisocana.PhoneApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if (CatastropheComboBox.SelectedItem != null)
            {

                lblSelectCastastrophe.Text = Assets.Resources.ApplicationStrings.SelectCatastrophe;
                lblSelectCastastrophe.Foreground = new SolidColorBrush(Color.FromArgb(255, 226, 226, 226));

                LightCatastrophe catastrophe = CatastropheComboBox.SelectedItem as LightCatastrophe;
                HelpRequestForm.CatastropheId = catastrophe.catastropheId;
                HelpRequestForm.CatastropheName = catastrophe.catastropheName;

                (App.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Views/InfoPage.xaml", UriKind.Relative));

            }
            else
            {

                lblSelectCastastrophe.Text = Assets.Resources.ApplicationStrings.SelectCatastropheValidationError;
                lblSelectCastastrophe.Foreground = new SolidColorBrush(Color.FromArgb(255, 200, 0, 0));
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            var client = new MobileServicesClient();
            client.GetCatastrophesCompleted += (s, ea) =>
            {
                if (!ea.Cancelled)
                    CatastropheComboBox.ItemsSource = ea.Result;
                else
                    CatastropheComboBox.ItemsSource = new List<string>() { "Connection Unavailable" };

                CatastropheComboBox.SelectedIndex = 0;
            };
            client.GetCatastrophesAsync();
        }
    }
}